package casa.depesos.app.com.activity;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.common.base.utils.SPHelper;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import casa.depesos.app.com.App;
import casa.depesos.app.com.databinding.ActivitySplashBinding;
import casa.depesos.app.com.dlg.BaseDialogFragment;
import casa.depesos.app.com.dlg.RightDlg;
import casa.depesos.app.com.utils.Constant;
import casa.depesos.app.com.utils.Report;
import casa.depesos.app.com.utils.Utils;

public class SplashActivity extends BaseActivity {

    ActivitySplashBinding binding;
    Timer timer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivitySplashBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                jump();
            }
        }, 3000);
        Report.show("ACTION_BB_001");
    }

    @Override
    protected boolean isBlack() {
        return false;
    }

    private void jump() {
        if (SPHelper.getInstance(this).getBoolean("agree", false)) {
            jump2();
        } else {
            Report.show("ACTION_VV_003");
            new RightDlg().cancelable(false).setOnDismissListener(new BaseDialogFragment.OnDismissListener() {
                @Override
                public void onDismiss(boolean isOk, Object value) {
                    if (isOk) {
                        Report.click("ACTION_BB_003");
                        SPHelper.getInstance(App.getApp()).putBooleanAndCommit("agree", true);
                        check();
                    } else {
                        finish();
                    }
                }
            }).show(getSupportFragmentManager(), "");
            Report.show("ACTION_BB_002");
        }
    }

    private void check() {
        if (true) {
            jump2();
            return;
        }
        if (!Constant.white && !Utils.hasPermission(this, Manifest.permission.READ_PHONE_STATE)) {
            Report.show("ACTION_BB_004");
            XXPermissions.with(this).permission(Permission.READ_PHONE_STATE).request(new OnPermissionCallback() {
                boolean deal = false;

                @Override
                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                    if (allGranted) {
                        Report.click("ACTION_BB_005");
                        jump2();
                    } else {
                        onDenied();
                    }
                }

                @Override
                public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                    onDenied();
                }

                void onDenied() {
                    if (deal) {
                        return;
                    }
                    deal = true;
                    Report.click("ACTION_BB_006");
                    jump2();
                }
            });
        } else {
            jump2();
        }
    }

    private void jump2() {
        if (TextUtils.isEmpty(Utils.getToken())) {
            Intent i = new Intent(this, LoginActivity.class);
            startActivity(i);
            finish();
        } else {
            Intent i = new Intent(this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
